package com.ly.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ly.common.core.bean.R;
import com.ly.common.core.utils.excel.ExcelResult;
import com.ly.common.core.utils.excel.ExcelUtil;
import com.ly.system.system.bean.entity.SysRoleIndex;
import com.ly.system.service.ISysRoleIndexService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 角色首页配置
 *
 * @author jeecg
 * @Date: 2022-03-25
 */
@Tag(name = "角色首页配置")
@Slf4j
@RestController
@RequestMapping("/sys/sysRoleIndex")
@RequiredArgsConstructor
public class SysRoleIndexController {

    private final ISysRoleIndexService sysRoleIndexService;

    /**
     * 添加
     *
     * @param entity
     * @return
     */
    @Operation(summary = "角色首页配置-添加")
    @PostMapping("/add")
    public R<String> add(@Validated @RequestBody SysRoleIndex entity) {
        return sysRoleIndexService.add(entity);
    }

    /**
     * 编辑
     *
     * @param entity
     * @return
     */
    @Operation(summary = "角色首页配置-编辑")
    @PostMapping("/edit")
    public R<String> edit(@Validated @RequestBody SysRoleIndex entity) {
        return sysRoleIndexService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return sysRoleIndexService.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return sysRoleIndexService.deleteBatch(ids);
    }


    /**
     * 导出excel
     *
     * @param response
     */
    @Operation(summary = "导出excel")
    @GetMapping("/exportXls")
    public void exportXls(HttpServletResponse response) {
        List<SysRoleIndex> list = sysRoleIndexService.list();
        ExcelUtil.exportExcel(list, "工作系数表", SysRoleIndex.class, response);
    }

    /**
     * 导入excel
     *
     * @param file
     * @param updateSupport：是否删除标识
     * @return
     */
    @Operation(summary = "导入excel")
    @PostMapping("/importExcel")
    public R<?> importWork(MultipartFile file, boolean updateSupport) throws Exception {
        // 监听： new ImportListener(updateSupport);
        ExcelResult<SysRoleIndex> result = ExcelUtil.importExcel(file.getInputStream(), SysRoleIndex.class, null);
        return R.ok(true, result.getAnalysis());
    }

    /**
     * 查询首页-roleId
     *
     * @param roleId
     * @return
     */
    @Operation(summary = "角色首页配置-roleId")
    @GetMapping("/getByRoleId")
    public R<?> getByRoleId(@RequestParam(name = "roleId") String roleId) {
        SysRoleIndex index = sysRoleIndexService.getOne(
                new LambdaQueryWrapper<SysRoleIndex>()
                        .eq(SysRoleIndex::getRoleId, roleId)
        );
        return R.ok(index);
    }

    /**
     * 首页配置-全局
     */
    @GetMapping("/queryDefIndex")
    public R<SysRoleIndex> queryDefIndex() {
        // todo: 查询自己的roleCode，得到对应的首页配置
        String roleCode = "global";
        SysRoleIndex defIndexCfg = sysRoleIndexService.queryDefaultIndex(roleCode);
        return R.ok(defIndexCfg);
    }

    /**
     * 首页配置-更新-全局
     */
    @PutMapping("/updateDefIndex")
    public R<?> updateDefIndex(SysRoleIndex entity) {
        sysRoleIndexService.updateDefaultIndex(entity);
        return R.ok(true, "创建成功");
    }
}
